package com.iapplize.gcm.test.server;

import java.io.IOException;

import java.util.Date;

import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;

@SuppressWarnings("serial")
public class GCM_Save_Client_Servlet extends HttpServlet {

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
String res = "";
		
		resp.setContentType("text/plain");

		String regId = req.getParameter("regId");
		String installationID = req.getParameter("installationID");
		String user_email = req.getParameter("user_email");

		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		Entity employee = new Entity("users",installationID);

		employee.setProperty("RegId", regId);
		employee.setProperty("Email", user_email);
		employee.setProperty("MacAddress", installationID);
		Date Updated_at = new Date();
		employee.setProperty("Updated_at", Updated_at);

		datastore.put(employee);

		res = "ok";
		
		/*resp.getWriter().println("regId : " + regId);
		resp.getWriter().println("installationID : " + installationID);
		resp.getWriter().println("user_email : " + user_email);*/
		
		if(res.equalsIgnoreCase("ok")){
			resp.getWriter().println("ok");
		}

	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		// TODO Auto-generated method stub
		resp.setContentType("text/plain");
		resp.getWriter().println("GCM Server");
	}
}
